package com.zhf.service.impl;

import com.zhf.dao.StudentDao;
import com.zhf.dao.impl.StudentDaoImpl;
import com.zhf.entity.Page;
import com.zhf.entity.Student;
import com.zhf.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();
    @Override
    public List<Student> queryAllStudent(Page<Student> page) {
        int count = studentDao.queryCount();
        page.setTotalCount(count);
        int begin = (page.getCurrentPage() - 1) * page.getPageSize();
        int size = page.getPageSize();
        List<Student> students = studentDao.queryAllStudent(begin, size);
        return students;
    }

    @Override
    public int insertStudent(Student student) {
        Student student1 = studentDao.queryStudentById(student.getId());
        if(student1 == null){
            // 如果新增学生学号不存在，则新增
            boolean b = studentDao.insertStudent(student);
            if(b){
                return 1;
            }
            else{
                return 0;
            }
        }
        return 0;
    }

    @Override
    public int updateStudent(Student student) {
        Student student1 = studentDao.queryStudentById(student.getId());
        if(student1 != null){
            // 如果新增学生学号不存在，则新增
            boolean b = studentDao.updateStudent(student);
            if(b){
                return 1;
            }
            else{
                return 0;
            }
        }
        return 0;
    }

    @Override
    public boolean deleteStudentList(String[] studentIds) {
        List<Student> studentList = studentDao.queryStudentByIds(studentIds);
        if(studentList != null && studentList.size() == studentIds.length){
            int rowIds = studentDao.deleteStudentByIds(studentIds);
            if(rowIds > 0){
                return true;
            }
        }
        else{
            return false;
        }
        return false;
    }

    @Override
    public List<Student> queryStudentByCodeAndName(String queryCode, String queryName) {
        List<Student> students = studentDao.queryStudentByIdAndName("%" + queryCode + "%", "%" + queryName + "%");
        return students;
    }
}
